﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using SenseTimeTracker.BusinessLayer;
using SenseTimeTracker.Misc;
using SenseTimeTracker.Properties;
using StedySoft.SenseSDK;
using StedySoft.SenseSDK.DrawingCE;

namespace SenseTimeTracker.Clients
{
	public partial class ClientEditForm : CommonSenseControls.CommonSenseForm
	{
		private Client client;

		private SensePanelTextboxItem clientNameItem;
		private SensePanelTextboxItem clientTypeItem;

		/// <summary>
		/// Initializes a new instance of the <see cref="ClientEditForm"/> class.
		/// </summary>
		public ClientEditForm()
		{
			InitializeComponent();

			senseHeaderCtrl.Text = Resources.ClientEditHeader;

			senseListCtrl.AddItem(new SensePanelDividerItem(string.Empty, Resources.ClientEditHeader));

			clientNameItem = new SensePanelTextboxItem();
			clientNameItem.LabelText = string.Format("{0}:", Resources.NameLabel);
			clientNameItem.Text = string.Empty;
			clientNameItem.MaxLength = DatabaseManager.MaxClientNameLength;
			clientNameItem.ShowSeparator = true;
			senseListCtrl.AddItem(clientNameItem);

			clientTypeItem = new SensePanelTextboxItem();
			clientTypeItem.LabelText = string.Format("{0}:", Resources.TypeLabel);
			clientTypeItem.Text = string.Empty;
			clientTypeItem.MaxLength = DatabaseManager.MaxClientTypeLength;
			clientTypeItem.ShowSeparator = true;
			senseListCtrl.AddItem(clientTypeItem);

			SIP.Enable(senseListCtrl.Handle);
		}

		/// <summary>
		/// Gets or sets the client
		/// </summary>
		public Client Client
		{
			get { return client; }
			set { client = value; }
		}

		protected override void OnLoad(EventArgs e)
		{
			base.OnLoad(e);

			clientNameItem.Text = client.Name;
			clientTypeItem.Text = client.Type;
		}

		protected override void OnClosing(CancelEventArgs e)
		{
			if (DialogResult == DialogResult.OK)
			{
				client.Name = clientNameItem.Text;
				client.Type = clientTypeItem.Text;

				// check for propper project name
				if (string.IsNullOrEmpty(client.Name))
				{
					if (SenseAPIs.SenseMessageBox.Show(Resources.DialogInvlidEmptyName, Resources.DialogHeaderInvalidInput, SenseMessageBoxButtons.RetryCancel) == DialogResult.Retry)
					{
						e.Cancel = true;
					}
					else
					{
						DialogResult = DialogResult.Cancel;
					}

					return;
				}
			}

			base.OnClosing(e);
		}

		/// <summary>
		/// Cancels client editing
		/// </summary>
		/// <param name="sender">sender object</param>
		/// <param name="e">cancel event arguments</param>
		private void MenuItemCancel_Click(object sender, EventArgs e)
		{
			DialogResult = DialogResult.Cancel;
		}

		/// <summary>
		/// Finishes client editing
		/// </summary>
		/// <param name="sender">sender object</param>
		/// <param name="e">finish event arguments</param>
		private void MenuItemOk_Click(object sender, EventArgs e)
		{
			DialogResult = DialogResult.OK;
		}
	}
}